package com.logica.tutorial.service;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.transaction.annotation.Transactional;

import com.logica.tutorial.dao.AddressDAO;
import com.logica.tutorial.dao.PersonDAO;
import com.logica.tutorial.dto.PersonDTO;
import com.logica.tutorial.model.Person;
import com.logica.tutorial.model.Sex;

public class PersonService {
	private PersonDAO personDAO;
	@SuppressWarnings("unused")
	private AddressDAO addressDAO;

	public void setPersonDAO(PersonDAO personDAO) {
		this.personDAO = personDAO;
	}

	public void setAddressDAO(AddressDAO addressDAO) {
		this.addressDAO = addressDAO;
	}

	@Transactional
	public Person getPersonById(Long id) {
		return personDAO.getPersonById(id);
	}

	@Transactional
	public Collection<Person> findPersonsByFirstName(String firstName) {
		return personDAO.findPersonsByFirstName(firstName);
	}

	@Transactional
	public Collection<Person> findPersonsInCity(String city) {
		return personDAO.findPersonsInCity(city);
	}

	@Transactional
	public void changeNameByID(Long idOfPersonToChange, String newFirstName,
			String newLastName) {
		personDAO.changeNameByID(idOfPersonToChange, newFirstName, newLastName);
	}

	@Transactional
	public long getNrOfPersons() {
		return personDAO.getNrOfPersons();
	}

	@Transactional
	public Collection<Person> findAllPersonsOfSex(Sex sex) {
		return personDAO.findAllPersonsOfSex(sex);
	}

	@Transactional
	public Collection<Person> findAllPersonsWithoutChildren() {
		return personDAO.findAllPersonsWithoutChildren();
	}

	@Transactional
	public Collection<Person> findPersonsWhereLastNameStartsWith(char character) {
		return personDAO.findPersonsWhereLastNameStartsWith(character);
	}

	@Transactional
	public void addPerson(Person person) {
		//TODO father/mother/friends
		if (person.getFather() != null) {
			Long fatherId = person.getFather().getId();
			if (isIdInUse(fatherId)) {
				person.setFather(getPersonById(fatherId));
			}
		}
		if (person.getMother() != null) {
			Long motherId = person.getMother().getId();
			if (isIdInUse(motherId)) {
				person.setMother(getPersonById(motherId));
			}
		}
		personDAO.addPerson(person);
	}

	@Transactional
	public void removePerson(Person person) {
		personDAO.removePerson(person);
	}

	@Transactional
	public boolean isIdInUse(Long personId) {
		return personDAO.isIdInUse(personId);
	}

	@Transactional
	public Collection<PersonDTO> findAllPersons() {
		Collection<Person> persons = personDAO.findAllPersons();
		Collection<PersonDTO> personDTOs = new ArrayList<PersonDTO>();

		for (Person person : persons) {
			personDTOs.add(new PersonDTO(person));
		}
		return personDTOs;
	}

	@Transactional
	public Collection<PersonDTO> findPerson(Person personToFind) {
		Collection<Person> persons = personDAO.findPerson(personToFind);
		Collection<PersonDTO> personDTOs = new ArrayList<PersonDTO>();

		for (Person person : persons) {
			personDTOs.add(new PersonDTO(person));
		}
		return personDTOs;
	}
}
